package com.ratta.supernote.plugin.sticker.lib.view.stickerList;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

public class StickerRecyclerView extends RecyclerView {
    private boolean mRequestedLayout = false;

    public StickerRecyclerView(@NonNull Context context) {
        super(context);
    }

    public StickerRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StickerRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void requestLayout() {
        super.requestLayout();
        // 强制post一个layout请求，确保重新布局
        if (!mRequestedLayout) {
            mRequestedLayout = true;
            post(() -> {
                mRequestedLayout = false;
                measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
                layout(getLeft(), getTop(), getRight(), getBottom());
            });
        }
    }
}
